home *** CD-ROM | disk | FTP | other *** search
- package com.opera;
-
- import java.applet.Applet;
- import java.applet.AppletContext;
- import java.applet.AppletStub;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.Panel;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.Hashtable;
- import java.util.Iterator;
- import java.util.StringTokenizer;
- import java.util.Vector;
-
- public class PluginPanel extends Panel implements AppletStub, Runnable {
- private static final int APPSTATE_UNKNOWN = 0;
- private static final int APPSTATE_LOADING = 1;
- private static final int APPSTATE_NOTFOUND = 2;
- private static final int APPSTATE_STOPPED = 3;
- private static final int APPSTATE_RUNNING = 4;
- private static final int APPSTATE_CRASHED = 5;
- private static final int APPSTATE_DESTROYED = 6;
- private static final int APPSTATE_INTERRUPTED = 7;
- private static final int APPSTATE_CLASSFORMATERROR = 8;
- private int appletState = 0;
- private Applet applet;
- private PluginContext context;
- private int inline_elm;
- private URL codeBase;
- private URL docBase;
- private Hashtable params;
- private Hashtable attr;
- private PluginClassLoader loader;
- private Thread loaderThread = null;
- private Thread destroyThread = null;
- private static Hashtable classLoaders = new Hashtable();
- private static int noRefMax = 10;
- private static Vector noRefLoaders = new Vector();
-
- protected static void setClassLoaderCacheSize(int var0) {
- if (var0 < 0) {
- var0 = 0;
- }
-
- noRefMax = var0;
- System.out.println("ClassLoader cache size: " + noRefMax);
- }
-
- private int getWidthParam() {
- if (this.attr != null) {
- String var1 = (String)this.attr.get("width");
- if (var1 != null) {
- return new Integer(var1);
- }
- }
-
- return 150;
- }
-
- private int getHeightParam() {
- if (this.attr != null) {
- String var1 = (String)this.attr.get("height");
- if (var1 != null) {
- return new Integer(var1);
- }
- }
-
- return 150;
- }
-
- public PluginPanel(PluginContext var1, Hashtable var2, Hashtable var3, URL var4, int var5) {
- try {
- this.setBackground(new Color(200, 200, 200));
- this.setLayout(new BorderLayout());
- this.context = var1;
- this.attr = var2;
- this.params = var3;
- this.docBase = var4;
- this.inline_elm = var5;
- String var6 = this.getParameter("java_codebase");
- if (var6 == null) {
- var6 = this.getParameter("codebase");
- }
-
- if (var6 != null) {
- if (var6.startsWith("file://localhost/")) {
- var6 = "file://" + var6.substring(16);
- }
-
- if (!var6.endsWith("/")) {
- var6 = var6 + "/";
- }
-
- try {
- this.codeBase = new URL(this.docBase, var6);
- } catch (MalformedURLException var12) {
- }
- }
-
- String var7 = this.docBase.getFile();
- URL var8 = null;
- int var9 = var7.lastIndexOf(47);
- if (var9 >= 0 && var9 < var7.length() - 1) {
- try {
- var8 = new URL(this.docBase, var7.substring(0, var9 + 1));
- } catch (MalformedURLException var11) {
- var11.printStackTrace(System.err);
- }
- }
-
- if (var8 == null) {
- var8 = this.docBase;
- }
-
- if (this.codeBase == null) {
- this.codeBase = var8;
- }
-
- String var10 = this.getParameter("java_archive");
- if (var10 == null) {
- var10 = this.getParameter("archive");
- }
-
- if (this.getParameter("cache_archive") != null) {
- if (var10 != null) {
- var10 = var10 + "," + this.getParameter("cache_archive");
- } else {
- var10 = this.getParameter("cache_archive");
- }
- }
-
- this.loader = getClassLoader(this.codeBase, var8, var10);
- this.loader.grab();
- if (var1 != null) {
- var1.setClassLoader(this.loader);
- }
- } catch (Exception var13) {
- var13.printStackTrace(System.err);
- }
-
- }
-
- static PluginClassLoader getClassLoader(URL var0, URL var1, String var2) {
- String var3 = var0.toString();
- if (var2 != null) {
- var3 = var3 + "," + var2.trim();
- }
-
- PluginClassLoader var4 = (PluginClassLoader)classLoaders.get(var3);
- if (var4 == null) {
- var4 = new PluginClassLoader(var0, var3);
- if (var2 != null) {
- StringTokenizer var5 = new StringTokenizer(var2, ",", false);
-
- while(var5.hasMoreTokens()) {
- String var6 = var5.nextToken().trim();
-
- try {
- var4.addJar(new URL(var0, var6));
- } catch (IllegalArgumentException var9) {
- var9.printStackTrace(System.err);
- } catch (MalformedURLException var10) {
- var10.printStackTrace(System.err);
- }
- }
- }
-
- var4.addJar(var0);
- if (var1 != var0) {
- var4.addJar(var1);
- }
-
- classLoaders.put(var3, var4);
- } else {
- noRefLoaders.remove(var4);
- }
-
- return var4;
- }
-
- static void cacheClassLoader(PluginClassLoader var0) {
- if (noRefMax <= 0) {
- classLoaders.remove(var0.getKey());
- } else {
- if (noRefLoaders.size() >= noRefMax) {
- classLoaders.remove(((PluginClassLoader)noRefLoaders.get(0)).getKey());
- noRefLoaders.remove(0);
- }
-
- noRefLoaders.add(var0);
- }
-
- }
-
- static void clearClassLoaderCache() {
- Iterator var0 = classLoaders.values().iterator();
-
- while(var0.hasNext()) {
- PluginClassLoader var1 = (PluginClassLoader)var0.next();
- if (var1.getUsageCount() == 0) {
- var0.remove();
- }
- }
-
- }
-
- static void printClassLoaders() {
- System.out.println("Classloaders:");
-
- for(PluginClassLoader var1 : classLoaders.values()) {
- System.out.println(var1.getKey());
- }
-
- }
-
- static Class findClass(String var0, String var1) {
- try {
- int var3 = var1.lastIndexOf(47);
- URL var2;
- if (var3 >= 0 && var3 < var1.length() - 1) {
- var2 = new URL(var1.substring(0, var3 + 1));
- } else {
- var2 = new URL(var1);
- }
-
- PluginClassLoader var4 = getClassLoader(var2, var2, (String)null);
- if (var4 != null) {
- var4.grab();
- var4.addJar(var2);
- ThreadGroup var6 = var4.getThreadGroup();
- FindClassThread var7 = new FindClassThread(var6, var4, var0);
- Class var5;
- synchronized(var7.syncObject) {
- var7.start();
-
- try {
- var7.syncObject.wait();
- } catch (InterruptedException var11) {
- }
-
- var5 = var7.found_class;
- }
-
- Object var14 = null;
- var4.release();
- return var5;
- } else {
- return null;
- }
- } catch (Exception var13) {
- var13.printStackTrace(System.out);
- return null;
- }
- }
-
- void resizeEmbedBox(int var1, int var2) {
- Dimension var3 = this.getSize();
- if (var3.getWidth() != (double)var1 || var3.getHeight() != (double)var2) {
- Frame var4 = (Frame)this.getParent();
- if (var4 != null) {
- var4.setSize(var1, var2);
- }
-
- this.setSize(var1, var2);
- if (this.applet != null) {
- this.applet.setSize(var1, var2);
- }
-
- this.validate();
- this.attr.put("width", String.valueOf(var1));
- this.attr.put("height", String.valueOf(var2));
- }
- }
-
- public void appletResize(int var1, int var2) {
- this.setSize(var1, var2);
- }
-
- public AppletContext getAppletContext() {
- return this.context;
- }
-
- public URL getCodeBase() {
- return this.codeBase;
- }
-
- public URL getDocumentBase() {
- return this.docBase;
- }
-
- public String getParameter(String var1) {
- String var2 = (String)this.params.get(var1.toLowerCase());
- if (var2 == null) {
- var2 = (String)this.attr.get(var1.toLowerCase());
- }
-
- return var2;
- }
-
- public boolean isActive() {
- return this.appletState == 4;
- }
-
- public void loadApplet() {
- try {
- ThreadGroup var1 = this.loader.getThreadGroup();
- this.loaderThread = new Thread(var1, this);
- this.loaderThread.setContextClassLoader(this.loader);
- this.loaderThread.start();
- } catch (Exception var2) {
- var2.printStackTrace(System.err);
- }
-
- }
-
- public void startApplet() {
- try {
- if (this.applet != null && this.appletState == 3) {
- this.applet.resize(this.getWidthParam(), this.getHeightParam());
- this.applet.start();
- this.appletState = 4;
- this.applet.validate();
- this.applet.setVisible(true);
- this.context.showStatus("Applet started.");
- }
- } catch (Exception var2) {
- var2.printStackTrace(System.err);
- if (this.appletState != 4) {
- this.appletState = 5;
- this.getParent().repaint();
- this.repaint();
- }
- }
-
- }
-
- public void stopApplet() {
- if (this.applet != null && this.appletState == 4) {
- try {
- this.applet.stop();
- } catch (Exception var2) {
- var2.printStackTrace(System.err);
- }
-
- this.appletState = 3;
- }
-
- }
-
- public void destroy() {
- if (this.context != null) {
- this.context.destroyPanel(this);
- }
-
- }
-
- public void destroyApplet() {
- try {
- if (this.loaderThread != null) {
- try {
- this.loaderThread.interrupt();
- } catch (Exception var3) {
- var3.printStackTrace(System.err);
- }
-
- this.loaderThread = null;
- }
-
- if (this.loader != null) {
- this.destroyThread = new Thread(this.loader.getThreadGroup(), this);
- this.destroyThread.setContextClassLoader(this.loader);
- this.destroyThread.start();
- ReleaseLoaderThread var1 = new ReleaseLoaderThread(this.loader);
- ((Thread)var1).start();
- }
- } catch (Exception var4) {
- var4.printStackTrace(System.err);
- } catch (Error var5) {
- var5.printStackTrace(System.err);
- }
-
- }
-
- public Applet getApplet() {
- return this.applet;
- }
-
- public void run() {
- if (Thread.currentThread() == this.destroyThread) {
- if (this.applet != null) {
- if (this.appletState == 4) {
- this.stopApplet();
- }
-
- try {
- this.applet.destroy();
- } catch (Exception var11) {
- var11.printStackTrace(System.err);
- }
-
- this.applet = null;
- this.appletState = 6;
- }
-
- this.destroyThread = null;
- } else {
- try {
- String var1 = this.getParameter("java_code");
- if (var1 == null) {
- var1 = this.getParameter("code");
- }
-
- if (var1 != null) {
- int var2 = var1.length();
- String var3 = var1;
- if (var2 > 6 && var1.startsWith(".class", var2 - 6)) {
- var3 = var1.substring(0, var2 - 6);
- }
-
- this.appletState = 1;
- this.getParent().repaint();
- this.repaint();
- Class var4 = this.loader.loadClass(var3);
- if (var4 != null) {
- this.applet = (Applet)var4.newInstance();
- if (this.applet != null) {
- this.appletState = 3;
- this.getParent().repaint();
- this.repaint();
- this.applet.setStub(this);
- this.applet.setVisible(false);
- this.add("Center", this.applet);
- this.getParent().validate();
- this.getParent().setSize(this.getWidthParam(), this.getHeightParam());
- this.setSize(this.getWidthParam(), this.getHeightParam());
- this.applet.resize(this.getWidthParam(), this.getHeightParam());
- this.context.showStatus("Initializing applet.");
-
- try {
- this.applet.init();
- this.getParent().validate();
- this.getParent().setVisible(true);
- this.setVisible(true);
- this.startApplet();
- } catch (Exception var6) {
- var6.printStackTrace(System.err);
- this.appletState = 5;
- this.getParent().repaint();
- this.repaint();
- }
- } else {
- this.appletState = 2;
- this.getParent().repaint();
- this.repaint();
- }
- } else {
- this.appletState = 2;
- this.getParent().repaint();
- this.repaint();
- }
- }
-
- this.loaderThread = null;
- } catch (ClassNotFoundException var7) {
- this.appletState = 2;
- this.getParent().repaint();
- this.repaint();
- var7.printStackTrace(System.err);
- this.loaderThread = null;
- } catch (Exception var8) {
- this.appletState = 5;
- this.getParent().repaint();
- this.repaint();
- var8.printStackTrace(System.err);
- this.loaderThread = null;
- } catch (ClassFormatError var9) {
- this.appletState = 8;
- this.getParent().repaint();
- this.repaint();
- var9.printStackTrace(System.err);
- } catch (Error var10) {
- this.appletState = 5;
- this.getParent().repaint();
- this.repaint();
- var10.printStackTrace(System.err);
- this.loaderThread = null;
- }
-
- if (this.context != null && this.inline_elm != 0) {
- this.context.setLoadingFinished(this.inline_elm);
- this.inline_elm = 0;
- }
- }
-
- }
-
- public void paint(Graphics var1) {
- try {
- if (this.appletState != 0 && this.appletState != 4) {
- FontMetrics var2 = var1.getFontMetrics();
- String var3;
- switch (this.appletState) {
- case 1:
- var3 = "Loading applet...";
- break;
- case 2:
- var3 = "Applet not found.";
- break;
- case 3:
- var3 = "Applet loaded.";
- break;
- case 4:
- default:
- var3 = "";
- break;
- case 5:
- var3 = "Applet crashed.";
- break;
- case 6:
- var3 = "Applet destroyed.";
- break;
- case 7:
- var3 = "Loading interrupted.";
- break;
- case 8:
- var3 = "Invalid bytecode.";
- }
-
- int var4 = var2.stringWidth(var3);
- int var5 = (this.getWidth() - var4) / 2;
- if (var5 < 0) {
- var5 = 0;
- }
-
- var1.drawString(var3, var5, this.getHeight() / 2);
- }
- } catch (Exception var6) {
- var6.printStackTrace(System.err);
- }
-
- }
-
- private static PluginPanel newPanel(int var0, int var1, int var2, String var3, String[] var4, String[] var5, String[] var6, String[] var7) {
- try {
- PluginContext var8 = PluginContextManager.getContext(var1);
- PluginPanel var9 = var8.newPanel(var4, var5, var6, var7, var3, var2);
- String var10 = System.getProperty("browser.opera.frameClassName");
- Frame var11 = createNewFrame(var10.replace('.', '/'), var0);
- var11.add(var9);
- var9.loadApplet();
- var11.setVisible(true);
- var9.setVisible(true);
- Graphics var12 = var9.getGraphics();
- var12.setFont(new Font("sans-serif", 0, 10));
- var9.repaint();
- return var9;
- } catch (Exception var13) {
- var13.printStackTrace(System.err);
- return null;
- }
- }
-
- private static native Frame createNewFrame(String var0, int var1);
- }
-